<?php
/*
*time：2017-02-24
*author:lisp
*/
class moments {
	public  function add_top($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$userName = Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId =  Session::$user['userId'];
		$userId = $userId?$userId:0;
		$actId= $param->actId;
		$momentsId = $param->momentsId;

		if($momentsId&&$momentsId > 0){
			$activity_info = moments::get_activity_info($actId,$customerId);
			if($activity_info['is_admin'] == 1){
				Session::switchCustomer(0);

				$top_db = M('mimoments_top');
				$map = array();
				$map['deleted'] = 0;
				$map['adminUserId'] = $userId;
				$map['momentsId'] = $momentsId;
				$map['activityId'] = $actId;
				$count = $top_db->where($map)->count();
				if($count > 0){
					$delete_result = $top_db->where($map)->setField('deleted',1);
					if($delete_result){

						$result['code'] = 1 ;
						$result['info'] = '删除置顶成功';
						$result['delete'] = 1;
					}else{
						$result['code'] = 0;
						$result['info'] = '删除置顶失败';
					}
				}else{
					$data =  array();
					$createTime = date('Y-m-d H:i:s');

					$data['activityId'] = $actId;
					$data['createTime'] = $createTime;
					$data['adminUserId'] = $userId;
					$data['momentsId'] = $momentsId;
					$add_top_result = $top_db->add($data);
					if($add_top_result){

						$result['code'] = 1;
						$result['delete'] = 0;

						$result['topId'] = $add_top_result;

					}else{

						$result['code'] = 0;

						if(!$add_top_result){
							$error .= $top_db->getError();
							$result['top_db_sql']  = $top_db->_sql();
						}
						$error = $error?$error:"发生未知错误";
						$result['info'] = $error;
					}
				}
			}else{

			}

		}else{
			$result['code'] = 0;
			$result['info'] = '留言编号不能为空';
		}
		Output::instance()->module('moments',"add_top")->object($result);
	}
	public function update_moments($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$userName = Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId =  Session::$user['userId'];
		$userId = $userId?$userId:0;
		$actId= $param->actId;
		$momentsId = $param->momentsId;
		$status = $param->status;
		if($momentsId&&$momentsId > 0){

			Session::switchCustomer(0);
			$activity_partake_db = M('miactivity_partake');
			$moments_db = M('mimoments');
			$activity_info = moments::get_activity_info($actId,$customerId);
			if($activity_info['is_admin'] == 1){
				$map = $save_data = array();
				$map['id'] = $momentsId;
				$save_data['status'] = $status;
				$save_data['adminUserId'] = $userId;
				$save_data['operationTime'] = date('Y-m-d H:i:s');
				$update_moments_result = $moments_db->where($map)->save($save_data);
				if($update_moments_result){
					$result['code'] = 1;
					$result['info'] = '操作成功';
				}else{
					$result['code'] = 0;
					$result['info'] = '操作失败';
				}
			}else{
				$result['code'] = 0;
				$result['info'] = '您不是活动管理员，不能进行此操作';
			}



		}else{
			$result['code'] = 0;
			$result['info'] = '留言编号不能为空';
		}
		Output::instance()->module('moments',"update_moments")->object($result);
	}
	//屏蔽和审核评论
	public function update_comment($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$userName = Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId =  Session::$user['userId'];
		$userId = $userId?$userId:0;
		$actId= $param->actId;
		$momentsId = $param->momentsId;
		$commentId = $param->commentId;
		$status = $param->status;
		if($commentId&&$commentId > 0){

			Session::switchCustomer(0);
			$activity_partake_db = M('miactivity_partake');
			$moments_db = M('mimoments_comment');
			$activity_info = moments::get_activity_info($actId,$customerId);
			if($activity_info['is_admin'] == 1){
				$map = $save_data = array();
				$map['id'] = $commentId;
				$save_data['status'] = $status;
				$save_data['adminUserId'] = $userId;
				$save_data['operationTime'] = date('Y-m-d H:i:s');
				$update_moments_result = $moments_db->where($map)->save($save_data);
				if($update_moments_result){
					$result['code'] = 1;
					$result['info'] = '操作成功';
					if($status == 1){
						$comment_map = array();
						$comment_map['deleted'] = 0;
						$comment_map['status'] = 1;
						$comment_map['id'] = $commentId;

						$comment_data = M('mimoments_comment')->where($comment_map)->find();
						if($comment_data){
							$momentsId = $comment_data['momentsId'];

							moments::add_read_record($momentsId,'comment',$comment_data,$param);
						}
					}
				}else{
					$result['code'] = 0;
					$result['info'] = '操作失败';
				}
			}else{
				$result['code'] = 0;
				$result['info'] = '您不是活动管理员，不能进行此操作';
			}



		}else{
			$result['code'] = 0;
			$result['info'] = '评论编号不能为空';
		}
		Output::instance()->module('moments',"update_comment")->object($result);
	}
	public function del_moments($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$userName = Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId =  Session::$user['userId'];
		$userId = $userId?$userId:0;
		$actId = $param->actId;
		$momentsId = $param->momentsId;
		$partakeId = $param->partakeId;
		$status = $param->status;
		if($momentsId&&$momentsId > 0 &&$partakeId && $partakeId > 0){

			Session::switchCustomer(0);
			$activity_partake_db = M('miactivity_partake');
			$moments_db = M('mimoments');
			$map = $save_data = array();
			$map['id'] = $momentsId;

			$map['partakeId'] = $partakeId;
			$map['userId'] = $userId;

			$save_data['deleted'] = 1;

			$moments_db->startTrans();
			$update_moments_result = $moments_db->where($map)->save($save_data);
			$map = array();
			$map['id'] = $partakeId;
			$map['userId'] = $userId;
			$map['activityId'] = $actId;
			$update_partake_result = $activity_partake_db->where($map)->setField('deleted',1);

			if($update_moments_result&&$update_partake_result){
				$moments_db->commit();
				$result['code'] = 1;
				$result['info'] = '操作成功';
			}else{
				$moments_db->rollback();
				$result['code'] = 0;
				$result['info'] = '操作失败';
			}

		}else{
			$result['code'] = 0;
			$result['info'] = '留言编号与参与编号均不能为空';
		}
		Output::instance()->module('moments',"del_moments")->object($result);

	}
	//删除评论
	public function del_comment($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$userName = Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId =  Session::$user['userId'];
		$userId = $userId?$userId:0;
		$actId = $param->actId;
		$momentsId = $param->commentId;
		$partakeId = $param->partakeId;
		$status = $param->status;
		if($momentsId&&$momentsId > 0 &&$partakeId && $partakeId > 0){

			Session::switchCustomer(0);
			$activity_partake_db = M('miactivity_partake');
			$moments_db = M('mimoments_comment');
			$map = $save_data = array();
			$map['id'] = $momentsId;

			$map['partakeId'] = $partakeId;
			$map['userId'] = $userId;

			$save_data['deleted'] = 1;

			$moments_db->startTrans();
			$update_moments_result = $moments_db->where($map)->save($save_data);
			$map = array();
			$map['id'] = $partakeId;
			$map['userId'] = $userId;
			$map['activityId'] = $actId;
			$update_partake_result = $activity_partake_db->where($map)->setField('deleted',1);

			if($update_moments_result&&$update_partake_result){
				$moments_db->commit();
				$result['code'] = 1;
				$result['info'] = '操作成功';
			}else{
				$moments_db->rollback();
				$result['code'] = 0;
				$result['info'] = '操作失败';
			}

		}else{
			$result['code'] = 0;
			$result['info'] = '留言编号与参与编号均不能为空';
		}
		Output::instance()->module('moments',"del_comment")->object($result);

	}
	private function add_read_record($momentsId,$type,$result,$param){
		$retVal = Session::init();
		$partakeId = $result['partakeId'];
		$userName = Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId =  Session::$user['userId'];
		// echo $userId ;
		// exit;
		if($momentsId > 0){
			Session::switchCustomer(0);
			$moments_map = array();
			$moments_map['deleted'] = 0;
			$moments_map['status'] = 1;
			$moments_map['id'] = $momentsId;
			$moments_user_id = M('mimoments')->where($moments_map)->getField('userId');
			// echo M('mimoments')->_sql();
			$comment_map = $praise_map = array();
			$comment_map['deleted'] = 0;
			$comment_map['status'] = 1;
			$comment_map['momentsId'] = $momentsId;
			$comment_user_list = M('mimoments_comment')->where($comment_map)->distinct(true)->getField('id,userId');
			$praise_map['deleted'] = 0;
			$praise_map['momentsId'] = $momentsId;
			$prasie_user_list = M('mimoments_praise')->where($praise_map)->distinct(true)->getField('id,userId');

			$user_list = array();
			$user_list[] = $moments_user_id;

			if($comment_user_list){
				$user_list = array_merge($user_list,$comment_user_list);
			}


			if($prasie_user_list){
				$user_list = array_merge($user_list,$prasie_user_list);
			}
			$user_list = array_unique($user_list);

			$read_data_list = $read_data = array();
			foreach ($user_list as $key => $value) {
				if($value != $userId&&$value!=0){
					$read_data['deleted'] = 0 ;
					$read_data['partakeId'] = $partakeId;
					$read_data['cid'] = $customerId;
					$read_data['userId'] = $value;
					$read_data['createTime'] = date('Y-m-d H:i:s');
					$read_data_list[] = $read_data;
				}
			}

			Log::write('read_data_list:'.json_encode($read_data_list));
			if($read_data_list&&count($read_data_list) > 0){
				M('mimoments_read')->addAll($read_data_list);
			}


		}

	}

	public  function addPraise($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}

		$userName = Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId =  Session::$user['userId'];
		$userId = $userId?$userId:0;
		$actId= $param->actId;
		$momentsId = $param->momentsId;
		$fromUserId = $param->fromUserId;
		$fromUserId = $fromUserId?$fromUserId:0;
		$channelId = $param->channelId;
		$is_admin = $param->is_admin;
		if($momentsId&&$momentsId > 0){
			Session::switchCustomer(0);
			$activity_partake_db = M('miactivity_partake');
			$praise_db = M('mimoments_praise');
			$map = array();
			$map['deleted'] = 0;
			$map['userId'] = $userId;
			$map['momentsId'] = $momentsId;
			$info = $praise_db->where($map)->find();
			if($info){
				$delete_partake_result = $activity_partake_db->where('id='.$info['partakeId'])->setField('deleted',1);
				$delete_result = $praise_db->where($map)->setField('deleted',1);
				if($delete_result&&$delete_partake_result){
					$result = moments::get_praise_list($momentsId,$customerId,$userId);
					$result['code'] = 1 ;
					$result['info'] = '删除点赞成功';
					$result['delete'] = 1;

				}else{
					$result['code'] = 0;

					$result['info'] = '删除失败';
				}
			}else{

				/*新增,获取ip对应的信息*/
				$ipInfo = self::getIp_agent();

				$data = $comment_data = array();
				$createTime = date('Y-m-d H:i:s');
				$data['cid'] = $customerId;
				$data['activityId'] = $actId;
				$data['createTime'] = $createTime;
				$data['userId'] = $userId;
				$data['fromUserId'] = $fromUserId;
				$data['channelId'] = $channelId;
				/*以下数据为新增*/
				$data['ip_address'] = $ipInfo['ip_address'];
				$data['ip_city'] = $ipInfo['city'];
				$data['ip_province'] = $ipInfo['province'];
				$data['user_agent'] = $ipInfo['userAgent'];

				$activity_partake_db->startTrans();
				$partakeId = $activity_partake_db->add($data);
				$comment_data['partakeId'] = $partakeId;
				$comment_data['createTime'] = $createTime;
				$comment_data['momentsId'] = $momentsId;
				$comment_data['userId'] = $userId;
				if($is_admin){
					$comment_data['isAdmin'] = $is_admin;
				}else{
					$comment_data['isAdmin'] = 0;
				}
				$add_comment_result = $praise_db->add($comment_data);
				if($partakeId&&$add_comment_result){
					$activity_partake_db->commit();
					$result = moments::get_praise_list($momentsId,$customerId,$userId);
					$result['code'] = 1;
					$result['delete'] = 0;
					$result['partakeId'] = $partakeId;
					$result['praiseId'] = $add_comment_result;
					moments::add_read_record($momentsId,'praise',$result,$param);

				}else{
					$activity_partake_db->rollback();
					$result['code'] = 0;
					if(!$partakeId){
						$error = $activity_partake_db->getError();
						$result['partake_sql'] = $activity_partake_db->_sql();
					}
					if(!$add_comment_result){
						$error .= $praise_db->getError();
						$result['praise_sql']  = $praise_db->_sql();
					}
					$error = $error?$error:"发生未知错误";
					$result['info'] = $error;
				}
			}
		}else{
			$result['code'] = 0;
			$result['info'] = '留言编号不能为空';
		}
		Output::instance()->module('moments',"addPraise")->object($result);
	}
	private function get_praise_list($momentsId,$customerId,$userId){


		$result = $praise_map  = array();
		Session::switchCustomer(0);
		$praise_db = M('mimoments_praise');
		$praise_map['a.momentsId'] = $momentsId;
		$praise_map['a.deleted'] = 0;
		$praise_list = M('mimoments_praise')->alias('a')
			->field('a.*,e.nickname,e.headimgurl')
			->join('inner join '.C('DB_PREFIX').'miactivity_partake c on a.partakeId=c.id')
			->join('inner join '.C('DB_PREFIX').$customerId.'_user_weixin_info e  on a.userId=e.userId')
			->where($praise_map)
			->order('a.createTime asc')
			->select();

		$result['is_praise'] = 0;
		foreach ($praise_list as $ke => $val) {
			if($val['userId'] == $userId){
				$result['is_praise'] = 1;
				break;
			}
		}
		$result['praise_list'] = $praise_list;
		return $result;
	}

	/*
	*首页
	*/
	public function index($param){

		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$result = array();
		$userName=Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId =  Session::$user['userId'];;

		$actId=$param->actId;
		$chanelId=$param->chanelId;

		$partake_id = moments::check_join($param);
		// if($partake_id){
		// 	$result['code'] = 1;
		// 	$result['partake_id'] = $partake_id;
		// }else{
		$result['code'] = 0;
		$result['openid'] = $userName;
		$activity_info = moments::get_activity_info($actId,$customerId);
		$result['activity_info'] = $activity_info;
		$result['userId'] = $userId;
		// /}


		Output::instance()->module('moments',"index")->object($result);

	}
	/*
    *获取参加过此活动的参与编号
    */
	public function check_join($param){
		$retVal = Session::init();
		$customerId = Session::$user['customerId'];
		$userId =  Session::$user['userId'];;

		$actId=$param->actId;
		$chanelId=$param->chanelId;
		// $userId = 22;
		$map = array();
		$map['userId'] = $userId;
		$map['activityId'] = $actId;
		$map['cid'] = $customerId;
		Session::switchCustomer(0);
		$activity_partake_db = M('miactivity_partake');

		$partake_id = $activity_partake_db->where($map)->getField('id');
		// dump($partake_id);
		// exit;
		// echo $activity_partake_db->_sql();
		// exit;
		return $partake_id;
	}

	/*
	*立即参加按钮所对应页面
	*/
	public function join_now($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$result = array();
		$userName=Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId = Session::$user['userId'];
		$actId=$param->actId;

		// $partake_id = moments::check_join($param);
		// if($partake_id){
		// 	$result['code'] = 1;
		// 	$result['partake_id'] = $partake_id;
		// }else{
		$result['code'] = 1;
		$activity_info = moments::get_activity_info($actId,$customerId);

		$result['activity_info'] = $activity_info;
		$result['userId'] =  $userId;
		$result['customerId'] = $customerId;
		//}
		$user_info = moments::get_user_info();
		$new_info = moments::get_new_info($param,false);
		$result['user_info'] = $user_info;
		$result['new_info'] = $new_info;
		Output::instance()->module('moments',"join_now")->object($result);
	}
	public function get_new_info($param,$is_normal=true){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$result = array();
		$userName=Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId = Session::$user['userId'];
		$actId=$param->actId;
		$map = array();

		$sql = '  h.deleted = 0 and  h.userId = '.$userId .' and a.activityId= '.$actId.' and  c.momentsId > 0 and c.deleted = 0 and c.status = 1   and d.deleted = 0 and d.status = 1  ';

		Session::switchCustomer(0);
		$list = M('mimoments_read')->alias('h')
			->field('d.*,e.nickname,e.headimgurl,c.userId as operationUserId,c.id as commentId,c.comment,c.createTime as commentTime,h.id as readId')
			->join('inner join '.C('DB_PREFIX').'miactivity_partake a on a.id=h.partakeId')
			->join('inner join '.C('DB_PREFIX').'mimoments_comment c on c.partakeId=h.partakeId')
			->join('inner join '.C('DB_PREFIX').'mimoments d on d.id = c.momentsId')
			->join('left join '.C('DB_PREFIX').$customerId.'_user_weixin_info e  on c.userId=e.userId')
			->where($sql)
			->distinct(true)
			->order('h.createTime desc,h.id desc')->select();

		Log::write('new_info_sql'. M('mimoments_read')->_sql());



		$new_info_list = $new_info = array();
		foreach ($list as $key => $value) {
			$new_info['momentsId'] = $value['id'];
			$new_info['nickname'] = $value['nickname'];
			$new_info['headimgurl'] = $value['headimgurl'];
			$new_info['momentsContent'] = $value['content'];

			$new_info['picUrls'] = $value['picUrls'];
			$new_info['createTime'] = $value['commentTime'];
			$new_info['type'] = 'comment';
			$new_info['content'] = $value['comment'];
			$new_info['infoId'] = $value['readId'];
			$new_info['operationUserId'] = $value['operationUserId'];
			$new_info_list['commentId_'.$value['commentId']] = $new_info;

		}

		$sql = ' h.deleted = 0 and  h.userId = '.$userId .' and a.activityId= '.$actId.' and  c.momentsId > 0 and c.deleted = 0 and d.deleted = 0 and d.status = 1  ';

		$list = M('mimoments_read')->alias('h')
			->field('d.*,e.nickname,e.headimgurl,c.id as praiseId,c.userId as operationUserId,c.createTime as praiseTime,h.id as readId')
			->join('inner join '.C('DB_PREFIX').'miactivity_partake a on a.id=h.partakeId')
			->join('inner join '.C('DB_PREFIX').'mimoments_praise c on c.partakeId=h.partakeId')
			->join('inner join '.C('DB_PREFIX').'mimoments d on d.id = c.momentsId')
			->join('left join '.C('DB_PREFIX').$customerId.'_user_weixin_info e  on c.userId=e.userId')
			->where($sql)
			->distinct(true)
			->order('h.createTime desc,h.id desc')->select();
		foreach ($list as $key => $value) {
			$new_info['momentsId'] = $value['id'];
			$new_info['nickname'] = $value['nickname'];
			$new_info['headimgurl'] = $value['headimgurl'];
			$new_info['momentsContent'] = $value['content'];

			$new_info['picUrls'] = $value['picUrls'];
			$new_info['createTime'] = $value['praiseTime'];
			$new_info['type'] = 'praise';
			$new_info['content'] = '';
			$new_info['infoId'] = $value['readId'];
			$new_info['operationUserId'] = $value['operationUserId'];
			$new_info_list['praiseId_'.$value['praiseId']] = $new_info;
		}


		$new_info_data = array();
		foreach ($new_info_list as $key => $value) {
			$new_info_data[] = $value;
		}


		$new_info_list = list_sort_by($new_info_data,'createTime','desc');



		$result['code'] = 1;
		$result['new_info_list'] = $new_info_list;

		if($is_normal){
			Session::switchCustomer(0);
			foreach ($new_info_list as $key => $value) {
				M('mimoments_read')->where('id='.$value['infoId'])->setField('deleted',1);

			}
			Output::instance()->module('moments',"get_new_info")->object($result);
		}else{
			return count($new_info_list);
		}


	}

	/*
	*获取留言列表数据
	*/
	public function get_moments_list($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$result = array();
		$userName=Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId = Session::$user['userId'];
		$actId=$param->actId;
		$start = $param->start;
		$limit = $param->limit;
		$momentsId = $param->momentsId;
		if(!isset($start)){
			$start  = 0;
		}
		if(!isset($limit)){
			$limit = 10;
		}
		Session::switchCustomer(0);

		$activity_info = moments::get_activity_info($actId,$customerId);
		$is_admin = $activity_info['is_admin'];
		$map = array();
		$map['c.activityId'] = $actId;
		$map["c.deleted"] = 0;
		if($is_admin != 1){
			$map['a.status'] = 1;
		}

		if($momentsId > 0){
			$map['a.id'] = $momentsId;
		}


		$list = M('mimoments')->alias('a')
			->field('a.*,c.channelId,c.activityId,e.nickname,e.headimgurl,f.id as topId')
			->join('inner join '.C('DB_PREFIX').'miactivity_partake c on a.partakeId=c.id')
			->join('left join '.C('DB_PREFIX').$customerId.'_user_weixin_info e  on a.userId=e.userId')
			->join('left join (select * from '.C('DB_PREFIX').'mimoments_top where deleted = 0) as  f on a.id=f.momentsId')
			->where($map)
			->distinct(true)
			->order('f.createTime desc,id desc')->limit($start.', '.$limit)->select();
		// echo M('mimoments')->_sql();
		// exit;
		if ($list) {
			$visit_data = array();
			// $visit_data['']
			$visit_data['partakeId'] = 0;
			$visit_data['userId'] = 0;
			moments::add_visit($param,$visit_data);
			$comment_map = $praise_map =  array();
			foreach ($list as $key => $value) {


				Session::switchCustomer(0);
				$value['content'] = html_entity_decode($value["content"]);
				// echo $value['content'];
				// exit;
				// var_dump($value);
				// exit;
				$moments_id = $value['id'];
				$comment_map['a.momentsId'] = $praise_map['a.momentsId'] = $moments_id;
				$comment_map['a.deleted'] = $praise_map['a.deleted'] = 0;
				if($is_admin != 1){
					$comment_map['a.status'] = 1;
				}

				$comment_list = M('mimoments_comment')->alias('a')
					->field('a.*,e.nickname,e.headimgurl')
					->join('inner join '.C('DB_PREFIX').'miactivity_partake c on a.partakeId=c.id')
					->join('left join '.C('DB_PREFIX').$customerId.'_user_weixin_info e  on a.userId=e.userId')
					->where($comment_map)
					->order('a.createTime asc')
					->select();

				foreach ($comment_list as $i => $comment) {
					$comment_list[$i]["comment"] = html_entity_decode($comment["comment"]);
					$replyId = $comment["replyId"];
					foreach($comment_list as $j =>$reply){
						if($replyId == $reply["id"]){
							$comment_list[$i]["replyName"] = $reply["nickname"];
						}
					}
				}
				$value['comment_list'] = $comment_list;

				$praise_list = M('mimoments_praise')->alias('a')
					->field('a.*,e.nickname,e.headimgurl')
					->join('inner join '.C('DB_PREFIX').'miactivity_partake c on a.partakeId=c.id')
					->join('left join '.C('DB_PREFIX').$customerId.'_user_weixin_info e  on a.userId=e.userId')
					->where($praise_map)
					->order('a.createTime asc')
					->select();

				// echo M('mimoments_comment')->_sql();
				// exit;
				//   echo M('mimoments_praise')->_sql();
				// exit;
				$value['is_praise'] = 0;
				foreach ($praise_list as $ke => $val) {
					if($val['userId'] == $userId){
						$value['is_praise'] = 1;
						break;
					}
				}
				$value['praise_list'] = $praise_list;
				$list[$key] = $value;
			}
			$count = M('mimoments')->alias('a')
				->field('a.*,c.channelId,c.activityId')
				->join('inner join '.C('DB_PREFIX').'miactivity_partake c on a.partakeId=c.id')
				->join('left join '.C('DB_PREFIX').$customerId.'_user_weixin_info e  on a.userId=e.userId')
				->where($map)
				->distinct(true)
				->count();
			Session::switchCustomer($customerId);
			$result['code'] = 1;
			$result['list'] = $list;
			$result['count'] = $count;
			$new_info = moments::get_new_info($param,false);
			$result['new_info'] = $new_info;

		} else {
			$result['code'] = 0;
			$result['info'] = '没有更多数据';
		}
		Output::instance()->module('moments',"get_moments_list")->object($result);
	}


	private function get_user_info(){
		$retVal = Session::init();
		$customerId = Session::$user['customerId'];
		$userId = Session::$user['userId'];
		// $userId = 17;
		Session::switchCustomer($customerId);
		$user_weixin_info_db = D('user_weixin_info');
		$user = D('user');
		$map = array();
		$map['userId'] = $userId;
		$user_info = $user_weixin_info_db->where($map)->find();
		//改变用户关注状态获取信息
		$map_user['id'] = $userId;
		$user_status = $user->where($map_user)->getField('status');
		if (($user_status == 'scansubscribe') || ($user_status == 'subscribe')){
			$user_info['subscribe'] = 1;
		}else{
			$user_info['subscribe'] = 0;
		}
		// echo $user_weixin_info_db->_sql();
		// exit;
		return $user_info;


	}
	/*

	*获取活动信息
	*/
	private function get_activity_info($actId,$customerId){
		$retVal = Session::init();
		// var_dump(Session::$user);
		// exit;
		$userName=Session::$user['userName'];
		$userId = Session::$user['userId'];
		// $userId = 17;
		Session::switchCustomer(0);
		$map = array();
		$map['deleted'] = 0;
		$map['fromCustomerId'] = $customerId;

		$activity_info = M("Miactivity")->field('deleted',true)->where($map)->find($actId);
		if($activity_info){
			$jsonstr = json_decode($activity_info['jsonstr'],true);
			$jsonstr['activity_index_img']['src'] = explode(',',$jsonstr['activity_index_img']['src']);

			$jsonstr['activity_submit_tip']['src'] = explode(',',$jsonstr['activity_submit_tip']['src']);

			$jsonstr['activity_rule_img']['src'] = explode(',',$jsonstr['activity_rule_img']['src']);
			$activity_info['jsonstr'] = $jsonstr;
			if($activity_info['accessTag'] && $activity_info['accessTag'] != ''){
				setAccessTags($activity_info['accessTag'],$userName,'moments',$activity_info['id']);
			}
			$admin_info = $jsonstr['admin_info'];
			$admin_list = $admin_info['list'];
			$activity_info['need_check'] = $jsonstr['need_check'];
			$activity_info['is_admin'] = 0;

			foreach ($admin_list as $key => $value) {
				if($value['userId'] == $userId){
					$activity_info['is_admin'] = 1;
					break;
				}
			}

		}
		return $activity_info;
	}
	private function add_visit($param,$moments_data){

		$retVal = Session::init();
		$userId = Session::$user['userId'];
		// $userId = 20;
		if($userId != $moments_data['userId']){
			$customerId = Session::$user['customerId'];
			$channelId = $param->chanelId;
			Session::switchCustomer(0);

			/*获取ip对应的信息*/
			$ipInfo = self::getIp_agent();

			$activity_visit_db = M('miactivity_visit');
			$visit_data = array();
			$actId = $param->actId;
			$partake_id = $moments_data['partakeId'];
			$channelId = $param->chanelId;
			$fromUserId = $param->fromUserId;
			$fromUserId = $fromUserId?$fromUserId:0;
			$visit_data['cid'] = $customerId;
			$visit_data['activityId'] = $actId;
			$visit_data['partakeId'] = $partake_id;
			$visit_data['channelId'] = $channelId;
			$visit_data['userId'] = $userId;
			$visit_data['fromUserId'] = $fromUserId;
			$visit_data['createTime'] = date('Y-m-d H:i:s');

			/*以下数据为新增*/
			$visit_data['ip_address'] = $ipInfo['ip_address'];
			$visit_data['ip_city'] = $ipInfo['city'];
			$visit_data['ip_province'] = $ipInfo['province'];
			$visit_data['user_agent'] = $ipInfo['userAgent'];
			$activity_visit_db->add($visit_data);
		}



	}
	/*
	*检测活动状态
	*/
	public  function check($param){
		$retVal = Session::init();
		if(!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$actId=$param->actId;
		$customerId = Session::$user['customerId'];
		Session::switchCustomer(0);
		$result = $map = array();
		$map = array();
		$map['deleted'] = 0;
		$map['id'] = $actId;
		$map['status'] = 1;
		$map['endTime'] = array('gt',date('Y-m-d H:i:s'));
		$map['startTime'] = array('lt',date('Y-m-d H:i:s'));
		$map['fromCustomerId'] = $customerId;
		$activity_info = M("Miactivity")->field('type,deleted',true)->where($map)->find();
		// echo M("Miactivity")->_sql();

		if($activity_info && is_array($activity_info)){
			if($activity_info['isLimitCount'] == 1){
				if($activity_info['limitCount'] <= $activity_info['partakeCount']){
					$result['code'] = 0;
					$result['info'] = '此活动参与人数已达上限，不可参加';
				}else{
					$result['code'] = 1;
				}
			}else{
				$result['code'] = 1;
			}

		}else{
			$result['code'] = 0;
			$result['info'] = '此活动尚未开始或已结束';
		}
		Output::instance()->module('moments',"check")->object($result);

	}
	/*
	*添加参加活动数据
	*/
	public function save_data($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$result = array();
		$userName=Session::$user['userName'];
		$customerId = Session::$user['customerId'];

		$actId=$param->actId;
		// var_dump($param);
		//       echo $actId;
		//       exit;
		$activity_info = moments::get_activity_info($actId,$customerId);
		// var_dump($activity_info);
		// exit;
		if($activity_info){
			$time_now = date('Y-m-d H:i:s');
			if($activity_info['status'] == 1 && $activity_info['startTime'] < $time_now &&  $activity_info['endTime'] > $time_now){
				if($activity_info['isLimitCount'] == 1){
					if($activity_info['limitCount'] > $activity_info['partakeCount']){
						$sql = 'lock table '.C('DB_PREFIX').'miactivity read';
						M()->execute($sql);
						$result = moments::deal_data($param,$activity_info['partakeCount']);
						$sql = 'unlock tables';
						M()->execute($sql);

					}else{
						$result['code'] = 0;
						$result['info'] = '此活动参与人数已达上限，不可参加';
					}
				}else{
					$result = moments::deal_data($param,$activity_info['partakeCount']);
				}

			}else{
				$result['code'] = 0;
				$result['info'] = '此活动尚未开始或已结束';
			}

		}else{
			$result['code'] = 0;
			$result['info'] = '活动信息不存在';
		}

		Output::instance()->module('moments',"save_data")->object($result);
	}
	public function publish_comment($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$result = array();
		$userName=Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId = Session::$user['userId'];
		$userId = $userId?$userId:0;
		$actId=$param->actId;
		$momentsId = $param->momentsId;
		$fromUserId = $param->fromUserId;
		$fromUserId = $fromUserId?$fromUserId:0;
		$comment = $param->comment;
		$is_admin = $param->is_admin;
		$channelId = $param->chanelId;
		$replyId = $param->replyId;
		Log::write('传过来的数据:'.json_encode($param));
		// var_dump($param);
		// echo $momentsId;
		// exit;
		if($momentsId&&$momentsId > 0){
			$activity_info = moments::get_activity_info($actId,$customerId);
			$is_admin = $activity_info['is_admin'];
			$need_check = $activity_info['need_check'];

			/*新增,获取ip对应的信息*/
			$ipInfo = self::getIp_agent();

			Session::switchCustomer(0);
			$activity_partake_db = M('miactivity_partake');
			$moments_db = M('mimoments');
			$activity_db = M('miactivity');
			$comment_db = M('mimoments_comment');

			$data = $comment_data = array();
			$createTime = date('Y-m-d H:i:s');
			$data['cid'] = $customerId;
			$data['activityId'] = $actId;
			$data['createTime'] = $createTime;
			$data['userId'] = $userId;

			$data['fromUserId'] = $fromUserId;
			$data['channelId'] = $channelId;
			/*以下数据为新增*/
			$data['ip_address'] = $ipInfo['ip_address'];
			$data['ip_city'] = $ipInfo['city'];
			$data['ip_province'] = $ipInfo['province'];
			$data['user_agent'] = $ipInfo['userAgent'];

			$activity_partake_db->startTrans();
			$partakeId = $activity_partake_db->add($data);

			$comment_data['partakeId'] = $partakeId;

			preg_match_all('/\[(.[^\[]*)\]/',$comment,$newStr);
			foreach($newStr[0] as $key=>$str){
				$faceboxInfo = moments::getFaceboxInfo($str);
				if($faceboxInfo){
					$url = $faceboxInfo->url;
					$SERVER_NAME = WeixinApi::getDomainName();
					$SERVER_NAME = $SERVER_NAME['SERVER_NAME'];
					$img = '<img src="'.$SERVER_NAME."/".$url.'" alt="'.$newStr[1][$key].'">';
					$comment = str_replace($str,$img,$comment);
				}
			}

			$comment_data['comment'] = $comment;
			$comment_data['createTime'] = $createTime;
			$comment_data['replyId'] = $replyId;
			$comment_data['momentsId'] = $momentsId;
			$comment_data['userId'] = $userId;
			if($is_admin){
				$comment_data['isAdmin'] = $is_admin;
				$comment_data['status'] = 1;
			}else{
				$comment_data['isAdmin'] = 0;
				$comment_data['status'] = 1;
				if($need_check == 1){
					$comment_data['status'] = 0;
				}

			}
			$add_comment_result = $comment_db->add($comment_data);
			if($partakeId&&$add_comment_result){

				$activity_partake_db->commit();
				$result['code'] = 1;
				$result['partakeId'] = $partakeId;
				$result['commentId'] = $add_comment_result;

				$param->content = $comment;
				moments::sendTmpInfo($activity_info,'comment',$result,$param);
				if($need_check <= 0){
					moments::add_read_record($momentsId,'comment',$result,$param);
				}

			}else{
				$activity_partake_db->rollback();
				$result['code'] = 0;
				if(!$partakeId){
					$error = $activity_partake_db->getError();
					$result['partake_sql'] = $activity_partake_db->_sql();
				}
				if(!$add_comment_result){
					$error .= $comment_db->getError();
					$result['comment_sql']  = $comment_db->_sql();
				}

				$error = $error?$error:"发生未知错误";
				$result['info'] = $error;
			}
		}else{
			$result['code'] = 0;
			$result['info'] = '留言编号不能为空';
		}


		Output::instance()->module('moments',"publish_comment")->object($result);
	}

	private function sendTmpInfo($activity_info,$type,$result,$param){
		if($activity_info){
			$retVal = Session::init();
			$userName=Session::$user['userName'];
			$customerId = Session::$user['customerId'];
			$data = array();
			$tplType = '';
			$need_check = $activity_info['need_check'];
			$is_admin = $activity_info['is_admin'];
			$actId = $param->actId;
			$jsonstr = $activity_info['jsonstr'];

			$admin_info = $jsonstr['admin_info'];
			$admin_list = $admin_info['list'];

			$momentsId = $param->momentsId;

			//需要审核并且发布留言的人不是管理员的情况下才会发送审核通知
			if($need_check == 1 && $is_admin <= 0){
				Session::switchCustomer($customerId);
				$nickname_map = array();
				$nickname_map['openid'] = $userName;
				$nickname = M('user_weixin_info')->where($nickname_map)->getField('nickname');
				Log::write('get_nickname:'.M('user_weixin_info')->_sql());
				$tplType = 'AuditNotice_OPENTM407369196';
				$first = '您有一条留言等待审核';
				if($type == 'comment'){
					$first = '您有一条评论等待审核';
				}
				$keyword1 = $nickname;
				$keyword2 = date('Y-m-d H:s:i');
				$keyword3 = strip_tags($param->content);
				$remark = '点击打开详情进行审核';
				$data['first'] = array('value'=> $first,'color'=>"#000000");
				$data['keyword1'] = array('value'=> $keyword1,'color'=>'#000000');
				$data['keyword2'] = array('value'=> $keyword2,'color'=>'#000000');
				$data['keyword3'] = array('value'=> $keyword3,'color'=>'#000000');
				$data['remark'] = array('value'=> $remark,"color"=>'#000000');
				$url = C('SERVER_HOST').'call.html?customerId='.$customerId.'&module=moments&method=join_now&params={"actId":'.$actId.',"momentsId":'.$momentsId.'}#/detail/'.$momentsId;
				// $url = urlencode($url);
				$log_data = $data;
				$log_data['url'] = $url;
				$log_data['admin_list'] = $admin_list;
				$log_data['tplType'] = $tplType;
				$log_data['customerId'] = $customerId;
				Log::write('moments_notice:'.json_encode($log_data));
				foreach ($admin_list as $key => $value) {
					TemplateMessage::send($value['openid'],$customerId,$tplType,$url,$data);
				}

			}else{
				if($type == "comment"){
					Session::switchCustomer(0);
					$map = array();
					$map['a.id'] = $momentsId;
					$momentsInfo = M('mimoments')->alias('a')
						->field('a.content,b.openid')
						->join('inner join wx_'.$customerId.'_user_weixin_info b on a.userId=b.userId')
						->where($map)->find();
					$tplType = 'ConsultationReplyMessage_OPENTM202109783';
					$first = '您有一条新的消息回复。';
					$momentsOpenid = $momentsInfo['openid'];
					$keyword1 = strip_tags($momentsInfo['content']);
					$keyword2 = strip_tags($param->content);

					$remark = '请点击查看详情';
					$data['first'] = array('value'=> $first,'color'=>"#000000");
					$data['keyword1'] = array('value'=> $keyword1,'color'=>'#000000');
					$data['keyword2'] = array('value'=> $keyword2,'color'=>'#000000');

					$data['remark'] = array('value'=> $remark,"color"=>'#000000');
					$url = C('SERVER_HOST').'call.html?customerId='.$customerId.'&module=moments&method=join_now&params={"actId":'.$actId.',"momentsId":'.$momentsId.'}#/detail/'.$momentsId;
					// $url = '';
					// $url = urlencode($url);
					$log_data = $data;
					$log_data['url'] = $url;
					$log_data['momentsOpenid'] = $momentsOpenid;
					$log_data['tplType'] = $tplType;
					$log_data['customerId'] = $customerId;
					Log::write('comment_notice:'.json_encode($log_data));
					TemplateMessage::send($momentsOpenid,$customerId,$tplType,$url,$data);

				}


			}
		}

	}
	/*
	*具体处理参加活动的数据
	*/
	public function deal_data($param,$partakeCount){
		$retVal = Session::init();
		$result = array();
		$userName=Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$customerId = $customerId?$customerId:'0';
		$userId = Session::$user['userId'];
		$userId = $userId?$userId:'0';
		$cid = $customerId;
		$fromUserId = $param->fromUserId;
		$fromUserId = $fromUserId?$fromUserId:'0';

		$channelId = $param->chanelId;
		$picUrls = $param->picUrls;
		$phone = $param->phone;
		$name = $param->name;
		$actId=$param->actId;
		$activityId = $actId;
		$lat = $param->lat;
		$lng = $param->lng;
		$address_detail = $param->address_detail;
		$is_admin = $param->is_admin;
		$adminUserId = $param->adminUserId;
		$content = $param->content;
		$content = urldecode($content);

		preg_match_all('/\[(.[^\[]*)\]/',$content,$newStr);
		foreach($newStr[0] as $key=>$str){
			$faceboxInfo = moments::getFaceboxInfo($str);
			if($faceboxInfo){
				$url = $faceboxInfo->url;
				$SERVER_NAME = WeixinApi::getDomainName();
				$SERVER_NAME = $SERVER_NAME['SERVER_NAME'];
				$img = '<img src="'.$SERVER_NAME."/".$url.'" alt="'.$newStr[1][$key].'">';
				$content = str_replace($str,$img,$content);
			}
		}
		$activity_info = moments::get_activity_info($actId,$customerId);
		$is_admin = $activity_info['is_admin'];
		$need_check = $activity_info['need_check'];

		/*新增,获取ip对应的信息*/
		$ipInfo = self::getIp_agent();

		Session::switchCustomer(0);
		$activity_partake_db = M('miactivity_partake');
		$moments_db = M('mimoments');
		$activity_db = M('miactivity');
		$partake_map = array();
		$partake_map['cid'] = $cid;
		$partake_map['userId'] = $userId;
		$partake_map['activityId'] = $activityId;
		$createTime = date('Y-m-d H:i:s');
		$count = $activity_partake_db->where($partake_map)->count();
		// if($count <= 0){
		$activity_partake_db->startTrans();
		$partake_data = $photo_share_data = array();
		$partake_data['cid'] = $cid;
		$partake_data['userId'] = $userId;
		$partake_data['fromUserId'] = $fromUserId;
		$partake_data['activityId'] = $activityId;
		$partake_data['channelId'] = $channelId;
		$partake_data['createTime'] = $createTime;
		$partake_data['lat'] = $lat;
		$partake_data['lng'] = $lng;
		$partake_data['address_detail'] = $address_detail;
		/*以下数据为新增*/
		$partake_data['ip_address'] = $ipInfo['ip_address'];
		$partake_data['ip_city'] = $ipInfo['city'];
		$partake_data['ip_province'] = $ipInfo['province'];
		$partake_data['user_agent'] = $ipInfo['userAgent'];

		$partake_result = $activity_partake_db->add($partake_data);
		$update_partakeCount = true;
		if($partake_result){
			$update_map = array();
			$update_map['fromCustomerId'] = $customerId;
			$update_map['id'] = $activityId;
			$partakeCount = intval($partakeCount) + 1;
			$update_data = array();
			$update_data['partakeCount'] = $partakeCount;
			$update_partakeCount = $activity_db->where($update_map)->save($update_data);
		}
		$photo_share_result = false;
		if($partake_result&&$partake_result>0){
			$photo_share_data['cid'] = $cid;
			$photo_share_data['userId'] = $userId;
			$photo_share_data['partakeId'] = $partake_result;

			$photo_share_data['picUrls'] = $picUrls;
			$photo_share_data['createTime'] = $createTime;
			$floor_map = array();
			$floor_map['c.activityId'] = $activityId;

			$floor = $moments_db->alias('a')
				->join('inner join '.C('DB_PREFIX').'miactivity_partake c on a.partakeId=c.id')
				->where($floor_map)->order('floor desc')->getField('floor');
			// echo $moments_db->_sql();
			// echo $floor;
			// exit;
			$floor = intval($floor) + 1;
			$photo_share_data['floor'] = $floor;

			$photo_share_data['isAdmin'] = $is_admin;

			$photo_share_data['content'] = $content;
			if($is_admin){
				$photo_share_data['adminUserId'] = $adminUserId;
				$photo_share_data['status'] = 1;
			}else{
				$photo_share_data['adminUserId'] = 0;

				$photo_share_data['status'] = 1;
				if($need_check == 1){
					$photo_share_data['status'] = 0;
				}
			}
			$moments_result = $moments_db->add($photo_share_data);
		}

		if($partake_result&&$moments_result&&$update_partakeCount){
			$activity_partake_db->commit();
			$result['code'] = 1;
			$result['partake_id'] = $partake_result;
			$result['moments_id'] = $moments_result;
			$param->content = $content;
			$param->momentsId = $moments_result;
			moments::sendTmpInfo($activity_info,'moments',$result,$param);
			//$result['userId'] = $userId;
		}else{
			$activity_partake_db->rollback();
			$result['activity_partake_sql'] = $activity_partake_db->_sql();
			$result['activity_sql'] = $activity_db->_sql();
			$result['moments_db_db_sql'] = $moments_db->_sql();
			$result['code'] = 0;
			if(!$partake_result){
				$error = $activity_partake_db->getError();
			}elseif(!$update_partakeCount){
				$error = $activity_db->getError();
			}else{
				$error = $moments_db->getError();
			}
			$error = $error?$error:"发生未知错误";
			$result['info'] = $error;
		}

		// }else{
		// 	$result['code'] = 0;
		// 	$result['info'] = '你已经参加过本次活动';
		// }
		return $result;

	}

	/*
	*晒图页面数据接口
	*/
	public function show($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$result = array();
		$userName=Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId = Session::$user['userId'];
		$actId=$param->actId;
		$partake_id = $param->partake_id;
		$channelId = $param->chanelId;
		$fromUserId = $param->fromUserId;

		$partake_id_self = moments::check_join($param);
		if($partake_id_self &&$partake_id_self > 0){
			$result['code'] = 1;
			$result['partake_id'] = $partake_id_self;
		}
		$activity_info = moments::get_activity_info($actId,$customerId);
		Session::switchCustomer(0);
		$activity_photo_share_db = M('miactivity_photo_share');
		$activity_visit_db = M('miactivity_visit');


		if($partake_id && $partake_id > 0){
			if($fromUserId != $userId){
				/*获取ip对应的信息*/
				$ipInfo = self::getIp_agent();

				$visit_data = array();
				$visit_data['cid'] = $customerId;
				$visit_data['activityId'] = $actId;
				$visit_data['partakeId'] = $partake_id;
				$visit_data['channelId'] = $channelId;
				$visit_data['userId'] = $userId;
				$visit_data['fromUserId'] = $fromUserId;
				$visit_data['createTime'] = date('Y-m-d H:i:s');
				/*以下数据为新增*/
				$visit_data['ip_address'] = $ipInfo['ip_address'];
				$visit_data['ip_city'] = $ipInfo['city'];
				$visit_data['ip_province'] = $ipInfo['province'];
				$visit_data['user_agent'] = $ipInfo['userAgent'];

				$activity_visit_db->add($visit_data);
			}
			$map = array();
			$map['partakeId'] = $partake_id;
			// $map['']
			$map['deleted'] = 0;
			$photo_share_info = $activity_photo_share_db->field('name,phone,createTime,picUrls,partakeId')->where($map)->find();
			if($photo_share_info&&is_array($photo_share_info)){
				$photo_share_info['picUrls'] = explode(',', $photo_share_info['picUrls']);
				$result['photo_share_info'] = $photo_share_info;
				$visit_map  = array();
				$visit_map['partakeId'] = $partake_id;
				$visit_map['cid'] = $customerId;
				$visit_map['activityId'] = $actId;

				$visit_list = $activity_visit_db->alias('a')
					->join('inner join wx_'.$customerId.'_user_weixin_info b on a.userId=b.userId')
					->field('b.nickname,b.headimgurl')
					->distinct(true)
					->where($visit_map)->select();
				// $result['sql'] = $activity_visit_db->_sql();
				$result['visit_list'] = $visit_list;
				$result['visit_count'] = count($visit_list);

				$result['activity_info'] = $activity_info;
				$result['userId'] =  $userId;

			}else{
				$result['code'] = 0;
				$result['info'] = '参加活动的信息不存在';
			}

		}else{
			$result['code'] = 0;
			$result['info'] = '参数不全';
		}


		Output::instance()->module('moments',"show")->object($result);
	}
	/*
	*测试
	*/
	public function test($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$result = array();
		$userName=Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		Output::instance()->module('moments',"test")->object($result);
	}
	public  function getFaceboxInfo($mark){
		$faceboxList = self::getFacebox(false);
		foreach($faceboxList as $face){
			if($face->mark==$mark){
				return $face;
			}
		}
		return null;
	}
	/*
    *获取表情
    */
	public function getFacebox($is_normal = true){
		$facebox = array();
		$face = new stdClass();
		$face->id=0;
		$face->mark = '[微笑]';
		$face->url = 'mp/assets/imgs/facebox/0.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=1;
		$face->mark = '[撇嘴]';
		$face->url = 'mp/assets/imgs/facebox/1.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=2;
		$face->mark = '[色]';
		$face->url = 'mp/assets/imgs/facebox/2.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=3;
		$face->mark = '[发呆]';
		$face->url = 'mp/assets/imgs/facebox/3.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=4;
		$face->mark = '[得意]';
		$face->url = 'mp/assets/imgs/facebox/4.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=5;
		$face->mark = '[流泪]';
		$face->url = 'mp/assets/imgs/facebox/5.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=6;
		$face->mark = '[害羞]';
		$face->url = 'mp/assets/imgs/facebox/6.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=7;
		$face->mark = '[闭嘴]';
		$face->url = 'mp/assets/imgs/facebox/7.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=8;
		$face->mark = '[睡]';
		$face->url = 'mp/assets/imgs/facebox/8.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=9;
		$face->mark = '[大哭]';
		$face->url = 'mp/assets/imgs/facebox/9.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=10;
		$face->mark = '[尴尬]';
		$face->url = 'mp/assets/imgs/facebox/10.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=11;
		$face->mark = '[发怒]';
		$face->url = 'mp/assets/imgs/facebox/11.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=12;
		$face->mark = '[调皮]';
		$face->url = 'mp/assets/imgs/facebox/12.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=13;
		$face->mark = '[呲牙]';
		$face->url = 'mp/assets/imgs/facebox/13.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=14;
		$face->mark = '[惊讶]';
		$face->url = 'mp/assets/imgs/facebox/14.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=15;
		$face->mark = '[难过]';
		$face->url = 'mp/assets/imgs/facebox/15.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=16;
		$face->mark = '[酷]';
		$face->url = 'mp/assets/imgs/facebox/16.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=17;
		$face->mark = '[冷汗]';
		$face->url = 'mp/assets/imgs/facebox/17.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=18;
		$face->mark = '[抓狂]';
		$face->url = 'mp/assets/imgs/facebox/18.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=19;
		$face->mark = '[吐]';
		$face->url = 'mp/assets/imgs/facebox/19.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=20;
		$face->mark = '[偷笑]';
		$face->url = 'mp/assets/imgs/facebox/20.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=21;
		$face->mark = '[可爱]';
		$face->url = 'mp/assets/imgs/facebox/21.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=22;
		$face->mark = '[白眼]';
		$face->url = 'mp/assets/imgs/facebox/22.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=23;
		$face->mark = '[傲慢]';
		$face->url = 'mp/assets/imgs/facebox/23.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=24;
		$face->mark = '[饥饿]';
		$face->url = 'mp/assets/imgs/facebox/24.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=25;
		$face->mark = '[困]';
		$face->url = 'mp/assets/imgs/facebox/25.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=26;
		$face->mark = '[惊恐]';
		$face->url = 'mp/assets/imgs/facebox/26.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=27;
		$face->mark = '[流汗]';
		$face->url = 'mp/assets/imgs/facebox/27.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=28;
		$face->mark = '[憨笑]';
		$face->url = 'mp/assets/imgs/facebox/28.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=29;
		$face->mark = '[大兵]';
		$face->url = 'mp/assets/imgs/facebox/29.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=30;
		$face->mark = '[奋斗]';
		$face->url = 'mp/assets/imgs/facebox/30.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=31;
		$face->mark = '[咒骂]';
		$face->url = 'mp/assets/imgs/facebox/31.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=32;
		$face->mark = '[疑问]';
		$face->url = 'mp/assets/imgs/facebox/32.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=33;
		$face->mark = '[嘘]';
		$face->url = 'mp/assets/imgs/facebox/33.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=34;
		$face->mark = '[晕]';
		$face->url = 'mp/assets/imgs/facebox/34.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=35;
		$face->mark = '[折磨]';
		$face->url = 'mp/assets/imgs/facebox/35.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=36;
		$face->mark = '[衰]';
		$face->url = 'mp/assets/imgs/facebox/36.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=37;
		$face->mark = '[骷髅]';
		$face->url = 'mp/assets/imgs/facebox/37.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=38;
		$face->mark = '[敲打]';
		$face->url = 'mp/assets/imgs/facebox/38.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=39;
		$face->mark = '[再见]';
		$face->url = 'mp/assets/imgs/facebox/39.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=40;
		$face->mark = '[擦汗]';
		$face->url = 'mp/assets/imgs/facebox/40.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=41;
		$face->mark = '[抠鼻]';
		$face->url = 'mp/assets/imgs/facebox/41.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=42;
		$face->mark = '[鼓掌]';
		$face->url = 'mp/assets/imgs/facebox/42.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=43;
		$face->mark = '[糗大了]';
		$face->url = 'mp/assets/imgs/facebox/43.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=44;
		$face->mark = '[坏笑]';
		$face->url = 'mp/assets/imgs/facebox/44.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=45;
		$face->mark = '[左哼哼]';
		$face->url = 'mp/assets/imgs/facebox/45.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=46;
		$face->mark = '[右哼哼]';
		$face->url = 'mp/assets/imgs/facebox/46.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=47;
		$face->mark = '[哈欠]';
		$face->url = 'mp/assets/imgs/facebox/47.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=48;
		$face->mark = '[鄙视]';
		$face->url = 'mp/assets/imgs/facebox/48.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=49;
		$face->mark = '[委屈]';
		$face->url = 'mp/assets/imgs/facebox/49.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=50;
		$face->mark = '[快哭了]';
		$face->url = 'mp/assets/imgs/facebox/50.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=51;
		$face->mark = '[阴险]';
		$face->url = 'mp/assets/imgs/facebox/51.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=52;
		$face->mark = '[亲亲]';
		$face->url = 'mp/assets/imgs/facebox/52.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=53;
		$face->mark = '[吓]';
		$face->url = 'mp/assets/imgs/facebox/53.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=54;
		$face->mark = '[可怜]';
		$face->url = 'mp/assets/imgs/facebox/54.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=55;
		$face->mark = '[菜刀]';
		$face->url = 'mp/assets/imgs/facebox/55.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=56;
		$face->mark = '[西瓜]';
		$face->url = 'mp/assets/imgs/facebox/56.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=57;
		$face->mark = '[啤酒]';
		$face->url = 'mp/assets/imgs/facebox/57.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=58;
		$face->mark = '[篮球]';
		$face->url = 'mp/assets/imgs/facebox/58.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=59;
		$face->mark = '[乒乓]';
		$face->url = 'mp/assets/imgs/facebox/59.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=60;
		$face->mark = '[咖啡]';
		$face->url = 'mp/assets/imgs/facebox/60.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=61;
		$face->mark = '[饭]';
		$face->url = 'mp/assets/imgs/facebox/61.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=62;
		$face->mark = '[猪头]';
		$face->url = 'mp/assets/imgs/facebox/62.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=63;
		$face->mark = '[玫瑰]';
		$face->url = 'mp/assets/imgs/facebox/63.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=64;
		$face->mark = '[凋谢]';
		$face->url = 'mp/assets/imgs/facebox/64.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=65;
		$face->mark = '[示爱]';
		$face->url = 'mp/assets/imgs/facebox/65.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=66;
		$face->mark = '[爱心]';
		$face->url = 'mp/assets/imgs/facebox/66.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=67;
		$face->mark = '[心碎]';
		$face->url = 'mp/assets/imgs/facebox/67.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=68;
		$face->mark = '[蛋糕]';
		$face->url = 'mp/assets/imgs/facebox/68.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=69;
		$face->mark = '[闪电]';
		$face->url = 'mp/assets/imgs/facebox/69.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=70;
		$face->mark = '[炸弹]';
		$face->url = 'mp/assets/imgs/facebox/70.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=71;
		$face->mark = '[刀]';
		$face->url = 'mp/assets/imgs/facebox/71.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=72;
		$face->mark = '[足球]';
		$face->url = 'mp/assets/imgs/facebox/72.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=73;
		$face->mark = '[瓢虫]';
		$face->url = 'mp/assets/imgs/facebox/73.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=74;
		$face->mark = '[便便]';
		$face->url = 'mp/assets/imgs/facebox/74.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=75;
		$face->mark = '[月亮]';
		$face->url = 'mp/assets/imgs/facebox/75.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=76;
		$face->mark = '[太阳]';
		$face->url = 'mp/assets/imgs/facebox/76.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=77;
		$face->mark = '[礼物]';
		$face->url = 'mp/assets/imgs/facebox/77.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=78;
		$face->mark = '[拥抱]';
		$face->url = 'mp/assets/imgs/facebox/78.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=79;
		$face->mark = '[强]';
		$face->url = 'mp/assets/imgs/facebox/79.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=80;
		$face->mark = '[弱]';
		$face->url = 'mp/assets/imgs/facebox/80.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=81;
		$face->mark = '[握手]';
		$face->url = 'mp/assets/imgs/facebox/81.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=82;
		$face->mark = '[胜利]';
		$face->url = 'mp/assets/imgs/facebox/82.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=83;
		$face->mark = '[抱拳]';
		$face->url = 'mp/assets/imgs/facebox/83.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=84;
		$face->mark = '[勾引]';
		$face->url = 'mp/assets/imgs/facebox/84.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=85;
		$face->mark = '[拳头]';
		$face->url = 'mp/assets/imgs/facebox/85.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=86;
		$face->mark = '[差劲]';
		$face->url = 'mp/assets/imgs/facebox/86.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=87;
		$face->mark = '[爱你]';
		$face->url = 'mp/assets/imgs/facebox/87.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=88;
		$face->mark = '[NO]';
		$face->url = 'mp/assets/imgs/facebox/88.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=89;
		$face->mark = '[OK]';
		$face->url = 'mp/assets/imgs/facebox/89.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=90;
		$face->mark = '[爱情]';
		$face->url = 'mp/assets/imgs/facebox/90.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=91;
		$face->mark = '[飞吻]';
		$face->url = 'mp/assets/imgs/facebox/91.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=92;
		$face->mark = '[跳跳]';
		$face->url = 'mp/assets/imgs/facebox/92.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=93;
		$face->mark = '[发抖]';
		$face->url = 'mp/assets/imgs/facebox/93.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=94;
		$face->mark = '[呕火]';
		$face->url = 'mp/assets/imgs/facebox/94.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=95;
		$face->mark = '[转圈]';
		$face->url = 'mp/assets/imgs/facebox/95.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=96;
		$face->mark = '[磕头]';
		$face->url = 'mp/assets/imgs/facebox/96.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=97;
		$face->mark = '[回头]';
		$face->url = 'mp/assets/imgs/facebox/97.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=98;
		$face->mark = '[跳绳]';
		$face->url = 'mp/assets/imgs/facebox/98.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=99;
		$face->mark = '[挥手]';
		$face->url = 'mp/assets/imgs/facebox/99.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=100;
		$face->mark = '[激动]';
		$face->url = 'mp/assets/imgs/facebox/100.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=101;
		$face->mark = '[街舞]';
		$face->url = 'mp/assets/imgs/facebox/101.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=102;
		$face->mark = '[献吻]';
		$face->url = 'mp/assets/imgs/facebox/102.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=103;
		$face->mark = '[右太极]';
		$face->url = 'mp/assets/imgs/facebox/103.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=104;
		$face->mark = '[左太极]';
		$face->url = 'mp/assets/imgs/facebox/104.gif';
		array_push($facebox,$face);
		$result = array();
		$result['code'] = 1;
		$result['list'] = $facebox;
		if($is_normal){
			Output::instance()->module('moments',"getFacebox")->object($result);
		}else{
			return $facebox;
		}


		// return $facebox;
	}

	/**
	 * 获取吐槽活动规则图片
	 * @param $param
	 */
	public function getRuleImages($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$result = array();
		$userName=Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		$userId =  Session::$user['userId'];;
		$actId=$param->actId;

		$result['openid'] = $userName;
		$activity_info = moments::get_activity_info($actId,$customerId);
		if(!$activity_info){
			Output::instance()->object(array(
				'code' => -1,
				'massage' => '活动不存在',
				'data' => '',
			));
			return;
		}

		$result['activity_rule_img'] = explode(',',$activity_info['jsonstr']['activity_rule_img']['src']);
		//$result['userId'] = $userId;
		/*echo '<pre>';
			var_dump($result);
		echo '<pre>';exit;*/

		Output::instance()->module('moments',"rule")->object(array(
			'code' => 0,
			'massage' => '获取数据成功',
			'data' => $result
		));
	}

	/**
	 * 获取用户的ip以及user_agent信息
	 * @return array
	 */
	protected  static function getIp_agent(){
		$ipInfo = array();
		if($_SERVER['HTTP_HTTP_X_REAL_IP']){
			//nginx 代理模式下，获取客户端真实IP
			$realIp = $_SERVER['HTTP_HTTP_X_REAL_IP'];
		}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
			//客户端的ip
			$realIp = $_SERVER['HTTP_CLIENT_IP'];
		}elseif (isset($_SERVER['HTTP_HTTP_X_FORWARDED_FOR'])) {
			//浏览当前页面的用户计算机的网关
			$arr = explode(',', $_SERVER['HTTP_HTTP_X_FORWARDED_FOR']);
			$pos = array_search('unknown',$arr);
			if(false !== $pos) unset($arr[$pos]);
			$realIp = trim($arr[0]);
		}elseif (isset($_SERVER['REMOTE_ADDR'])) {
			//浏览当前页面的用户计算机的ip地址
			$realIp = $_SERVER['REMOTE_ADDR'];
		}else{
			$realIp=$_SERVER['REMOTE_ADDR'];
		}
		$userAgent = $_SERVER['HTTP_USER_AGENT'];
		$realIpInt = bindec(decbin(ip2long($realIp)));

		//查询ip对应的地址信息
		if($realIp){
			$cid = Session::$user['customerId'];
			Session::switchCustomer(0);
			$ipInfo_table = M('ip_info');

			$map = array();
			$map['start_ip_int'] = array('elt',$realIpInt);
			$map['end_ip_int'] = array('egt',$realIpInt);
			$result = $ipInfo_table->where($map)->find();
			if($result){
				$ipInfo['province'] = $result['province'];
				$ipInfo['city'] = $result['city'];
			}else{
				Log::write('客户端ip信息----:'.print_r($_SERVER, true));
				Log::write('查询ip信息-----'.$ipInfo_table->getLastSql());
			}
			Session::switchCustomer($cid);
		}

		$ipInfo['ip_address'] = $realIp;
		$ipInfo['userAgent'] = $userAgent;
		return $ipInfo;
	}
}